package com.mp.domain;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import lombok.Data;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Data
public class TemplateMessage {
    static Map<JsonNodeType,String> types=new HashMap<>(16);
    static {
        types.put(JsonNodeType.STRING,"String");
        types.put(JsonNodeType.OBJECT,"Object");
        types.put(JsonNodeType.POJO,"Object");
        types.put(JsonNodeType.BOOLEAN,"Object");
        types.put(JsonNodeType.ARRAY,"List");
        types.put(JsonNodeType.NUMBER,"Double");
    }
    public static void main(String[] args) throws JsonProcessingException {
        String json="{\n" +
                "           \"touser\":\"OPENID\",\n" +
                "           \"template_id\":\"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY\",\n" +
                "           \"url\":\"http://weixin.qq.com/download\",  \n" +
                "           \"miniprogram\":{\n" +
                "             \"appid\":\"xiaochengxuappid12345\",\n" +
                "             \"pagepath\":\"index?foo=bar\"\n" +
                "           },\n" +
                "           \"client_msg_id\":\"MSG_000001\",\n" +
                "           \"data\":{\n" +
                "\n" +
                "                   \"keyword1\":{\n" +
                "                       \"value\":\"巧克力\",\n" +
                "                       \"attrs\":[0,1,2]\n" +

                "                   },\n" +
                "                   \"keyword2\": {\n" +
                "                       \"value\":39.8\n" +
                "                   },\n" +
                "                   \"keyword3\": {\n" + "\"value\":\"2014年9月22日\",\"isCheck\":true}}}";
        ObjectMapper objectMapper=new ObjectMapper();
        JsonNode jsonObject = objectMapper.readTree(json);
        getObject(jsonObject,"");

    }
    static void getObject(JsonNode jsonObject,String pre){
        Iterator<Map.Entry<String, JsonNode>> fields = jsonObject.fields();
        while (fields.hasNext()){
            Map.Entry<String, JsonNode> next = fields.next();
            JsonNode value= next.getValue();
            if(value.isArray() ){
                System.out.println(types.get(value.getNodeType())+" "+next.getKey());
                getObject(value,pre+" ");
                continue;
            }

            if(value.isObject()){
                System.out.println(types.get(value.getNodeType())+" "+next.getKey());
                getObject(value,pre+" ");
                continue;
            }

            System.out.println(pre+types.get(value.getNodeType())+" "+next.getKey());
        }
    }
}
